313B - Ilya and Queries - CodeForces Solution


dp implementation *1100

Please click on ads to support us..

Python Code:

from bisect import *
from collections import *
from itertools import *
from heapq import *
from sys import stdin,setrecursionlimit
from functools import lru_cache
setrecursionlimit(10**7)

lines=stdin.readlines()
lines.reverse()
setrecursionlimit(10**8)
def input():
    return lines.pop().strip("\n")
s=list(input())
a=[0]
for i in range(1,len(s)):
    if s[i]==s[i-1]:
        a.append(1)
    else:
        a.append(0)
a=list(accumulate(a))
q=int(input())
for i in range(q):
    l,r=map(int,input().split())
    print(a[r-1]-a[l-1])
    	 		  	  		  		 	 	   		 	

C++ Code:

    #include<bits/stdc++.h>

    using namespace std;
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/tree_policy.hpp>

    #define ll long long
    # define pii pair<int , int >
    #define pll pair<long long, long long>
    #define vi vector<int> 
    #define vll vector<long long>
    #define mii map<int, int>
    #define si set<int>
    #define sc set<char>

    /* FUNCTION */
    #define f(i, s, e) for(long long i=s; i<e; i++)
    #define fe(i, s, e) for(long long i=s; i<=e; i++)
    #define rf(i, e, s) for(long long i=e-1; i>=s; i--)
    #define pb push_back
    #define eb emplace_back

    /* PRINTS */
    template <class T>
    void print_v(vector<T> &v){cout<<"{"; for(auto x:v) cout<<x<<","; cout<<"\b";}

    /* UTILS */
    #define MOD 1000000007
    #define pi 3.14159
    ll min(ll a, int b) {if(a<b) return a; return b;}
    ll min(int a, ll b) {if (a<b) return a; return b;}
    ll max(ll a, int b) { if (a>b) return a; return b; }
    ll max(int a,ll b) { if (a>b) return a; return b; }
    ll gcd(ll a,ll b) { if (b==0) return a; return gcd(b, a%b); }
    ll lcm(ll a,ll b) { return a/gcd(a,b)*b; }
    string to_upper(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A'; return a; }
    string to_lower(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A'; return a; }
    bool prime(ll a) { if (a==1) return 0; for (int i=2;i<=round(sqrt(a));++i) if (a%i==0) return 0; return 1; }
    void yes() { cout<<"YES\n"; }
    void no() { cout<<"NO\n"; }

    typedef long int int32;
    typedef unsigned long int uint32;
    typedef long long int int64;
    typedef unsigned long long int  uint64;
    typedef vector<ll> vl;
    typedef vector<char> vc;
    typedef vector<string> vs;

    template <typename T>
    void add(T& a, T b, T M) {
        a = ((a % M) + (b % M)) % M;
    }
     
    template <typename T>
    void mul(T& a, T b, T M) {
        a = ((a % M) * (b % M)) % M;
    }
     
    template <typename T>
    void sub(T& a, T b, T M) {
        a = (a - b + M) % M;
    }

    /* ------------------------------------------ OM GAN GANPATYAY NAMAH -------------------------------------------------*/

    // int solve(ll n){
        
    // }
    // map<int,int>mp;
    ll M=1e9+7;



    void solve(int x){
        if(x<0) return;
        ll a=sqrt(x);
        if(a*a!=x)a++;
        a*=a;
        solve(a-x-1);
        for(int i=x; i>=a-x; i--){
            cout<<i<<" ";
        }

            }

    int main(){
       

        #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin); //file input.txt is opened in reading mode i.e "r"
        freopen("output.txt","w",stdout);  //file output.txt is opened in writing mode i.e "w"
        #endif

        ios_base::sync_with_stdio(false);
        cin.tie(NULL);   
        cout.tie(NULL);
        // map<int,int> mp;
    //     ll t;
    //     cin>>t;


    //     while(t--){
                string s;
                cin>>s;
                vector<ll> pref(s.length()+1);
                pref[1]=0;
                for(ll i=1; i<s.length(); i++){
                    if(s[i]==s[i-1]) {
                        pref[i+1]=pref[i]+1;
                    }
                    else pref[i+1]=pref[i];
                }
                ll n;
                cin>>n;
                while(n--){
                    ll a,b;
                    cin>>a>>b;
                    // cout<<pref[b]<<" "<<pref[a]<<", ";
                    cout<<pref[b]-pref[a]+0ll<<endl;


                }
        

    // }
        return 0;}


Comments

Submit
0 Comments
More Questions

2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets